//
//  SISSMessagePassengerReccommendViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/6/28.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMessagePassengerReccommendViewController.h"
#import "SISSComboBoxView.h"
#import "SISSRecommandPassengerCell.h"
#import "SISSJsonRequestNetworkingManager+Resources.h"
#import "SISSPassengerResourceDetailVC.h"

static NSString *passengerCellID = @"passengerCellID";
@interface SISSMessagePassengerReccommendViewController ()<UITableViewDelegate,UITableViewDataSource,SISSRecommandPassengerCellDelegate,SISSComboBoxViewDelegate>


/** 客源模型数组 */
@property (strong, nonatomic) NSArray<SISSPassengerResourceModel *> *passengerSourcesArr;
/** tableView */
@property (weak, nonatomic) UITableView *tableView;
/* 底部推荐按钮 */
@property (weak, nonatomic) UIButton *recommendButton;


/** 可见范围 */
@property (assign, nonatomic) NSInteger visibleRange;
/** 排序规则 */
@property (assign, nonatomic) NSInteger orderAttrs;
/** 区域 */
@property (copy, nonatomic) NSString *region;
/** 街道办 */
@property (copy, nonatomic) NSString *regionDetail;
/** 价格 */
@property (copy, nonatomic) NSString *price;
/** 户型 */
@property (copy, nonatomic) NSString *houseType;
/** 面积 */
@property (copy, nonatomic) NSArray *area;
/** 朝向 */
@property (copy, nonatomic) NSArray *direction;
/** 装修程度 */
@property (copy, nonatomic) NSArray *decoration;
/** pageNo */
@property (assign, nonatomic) NSInteger pageNo;

/* selectIndexPath */
@property (assign, nonatomic) NSIndexPath *selectIndexPath;

@end

@implementation SISSMessagePassengerReccommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestPassengerResourceData];
}

#pragma mark - SISSComboBoxViewDelegate
- (void)comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title {
    
    if ([rootItemTitle isEqualToString:@"预算"]) {
        self.price = title;
    } else if ([rootItemTitle isEqualToString:@"户型"]) {
        self.houseType = title;
    }
    [self requestPassengerResourceData];
}

- (void)comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title subtitle:(NSString *)subtitle {
    
    self.region = title;
    self.regionDetail = subtitle;
    [self requestPassengerResourceData];
}

- (void)comboBoxViewDidSelectDisplayTypeFiltersWithTitle:(NSString *)title subtitles:(NSArray *)subtitles {
    
    if ([title isEqualToString:@"可见范围"]) {
        self.visibleRange = 2;
    } else if ([title isEqualToString:@"面积"]) {
        self.area = subtitles;
    } else if ([title isEqualToString:@"朝向"]) {
        self.direction = subtitles;
    } else if ([title isEqualToString:@"装修程度"]) {
        self.decoration = subtitles;
    }
    
}

- (void)comboBoxViewDisplayTypeFiltersConfirmClickAction {
    
    [self requestPassengerResourceData];
    self.visibleRange = 1;
    self.area = @[];
    self.direction = @[];
    self.decoration = @[];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.passengerSourcesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SISSRecommandPassengerCell *cell = [tableView dequeueReusableCellWithIdentifier:passengerCellID];
    cell.delegate = self;
    cell.isChooseMain = YES;
    cell.passengerResourceModel = self.passengerSourcesArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSPassengerResourceModel *model = self.passengerSourcesArr[indexPath.row];
    
    SISSPassengerResourceDetailVC *vc = [[SISSPassengerResourceDetailVC alloc] init];
    vc.title = @"客源详情";
    vc.isHideBottomButton = YES;
    vc.passengerModel = model;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - SISSRecommandPassengerCellDelegate
- (BOOL)recommandPassengerCell:(SISSRecommandPassengerCell *)cell checkButtonIsSelected:(BOOL)isSelected {
    
    [self.recommendButton setBackgroundColor: isSelected ? COLOR(0x349DC5) : COLOR(0x8998A5)];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.passengerSourcesArr[self.selectIndexPath.row].isSelected = NO;
    self.passengerSourcesArr[indexPath.row].isSelected = YES;
    self.selectIndexPath = indexPath;
    
    [self.tableView reloadData];
    return NO;
}

#pragma mark - 推荐按钮点击事件
- (void)recommendButtonClickAction {
    
    if (!self.selectIndexPath) return;
    if (self.recommendBlock) {
        self.recommendBlock(self.passengerSourcesArr[self.selectIndexPath.row]);
    }
    
//    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    
    [self requestPassengerResourceData];
}

- (void)reloadMoreData {
    
    [self requestPassengerResourceData];
}

#pragma mark - 加载数据
- (void)requestPassengerResourceData {
    
    // 非空处理
    //    NSString *keyword = self.keyword ? self.keyword : @"";
    //    NSInteger visibleRange = self.visibleRange == 2 ? self.visibleRange : 1;
    //    NSInteger orderAttrs = self.orderAttrs ? self.orderAttrs : 0;
    NSString *region = self.region ? self.region : @"不限";
    NSString *regionDetail = self.regionDetail ? self.regionDetail : @"不限";
    NSString *price = self.price ? self.price : @"";
    NSString *houseType = self.houseType ? self.houseType : @"不限";
    
    NSDictionary *parameters = @{
                                 @"spId":[SISSUserAccount shared].spId,
                                 @"region":region,
                                 @"regionDetail":regionDetail,
                                 @"price":price,
                                 @"houseType":houseType,
                                 @"area":self.area ,
                                 @"direction":self.direction,
                                 @"decoration":self.decoration
                                 };
    
    [kJsonRequestNetworkingManager requestEntrustPassengerResourceWithParameters:parameters success:^(NSArray<SISSPassengerResourceModel *> *passengerResources) {
        
        self.passengerSourcesArr = passengerResources;
        
        // 回到主线程刷新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    
    [super setupSubviews];
    
    // 添加筛选视图
    SISSComboBoxView *filterView = [[SISSComboBoxView alloc] initWithFrame:CGRectMake(0, self.navigationHeight, SCREEN_WIDTH, 44) isHouseResource:NO isCooperation:NO];
    filterView.delegate = self;
    
    [self.view addSubview:filterView];
    self.filterView = filterView;
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 80;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [tableView registerClass:[SISSRecommandPassengerCell class] forCellReuseIdentifier:passengerCellID];
    
    [self.view addSubview:tableView];
    
    // 推荐
    UIButton *recommendButton = [[UIButton alloc] init];
    [recommendButton setTitle:@"推荐" forState:UIControlStateNormal];
    [recommendButton setBackgroundColor:COLOR(0x8998A5)];
    [recommendButton addTarget:self action:@selector(recommendButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:recommendButton];
    self.recommendButton = recommendButton;
    
    
    // 自动布局
    CGFloat recommendButtonHeight = 47;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.filterView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(recommendButton.mas_top);
    }];
    
    [recommendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(recommendButtonHeight);
    }];
    
    
    // 记录控件
    self.tableView = tableView;
}


#pragma mark - Getter & Setter
- (NSArray<SISSPassengerResourceModel *> *)passengerSourcesArr {
    
    if (!_passengerSourcesArr) {
        _passengerSourcesArr = [NSArray array];
    }
    return _passengerSourcesArr;
}

- (NSArray *)area {
    
    if (!_area) {
        _area = [NSArray array];
    }
    return _area;
}



- (NSArray *)direction {
    
    if (!_direction) {
        _direction = [NSArray array];
    }
    return _direction;
}

- (NSArray *)decoration {
    
    if (!_decoration) {
        _decoration = [NSArray array];
    }
    return _decoration;
}



@end
